import 'package:chewie/chewie.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:go_router/go_router.dart';
import 'package:video_player/video_player.dart';
import 'package:wan_android/router/router_config.dart';

class ShortVideoWidget extends StatefulWidget {
  const ShortVideoWidget({super.key});

  @override
  State<ShortVideoWidget> createState() => _ShortVideoWidgetState();
}

class _ShortVideoWidgetState extends State<ShortVideoWidget> {
  TextEditingController _textController = TextEditingController();
  late VideoPlayerController _videoPlayerController;
  ChewieController? _chewieController;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    _videoPlayerController = VideoPlayerController.asset('assets/videos/movie.mp4')
      ..initialize().then((_) {
        // _videoPlayerController.play();
        setState(() {}); // 确保初始化完成后更新界面
      });

    _chewieController = ChewieController(
      videoPlayerController: _videoPlayerController,
      aspectRatio: _videoPlayerController.value.aspectRatio,
      autoPlay: false,
      looping: true,
      // customControls: 自定义控件面板
      // // 安卓中进度条样式
      // materialProgressColors: ChewieProgressColors(
      //   playedColor: Colors.red,
      //   handleColor: Colors.blue,
      //   backgroundColor: Colors.grey,
      //   bufferedColor: Colors.lightGreen,
      // ),
      // // ios 中进度条样式
      // cupertinoProgressColors: ChewieProgressColors(
      //   playedColor: Colors.red,
      //   handleColor: Colors.blue,
      //   backgroundColor: Colors.grey,
      //   bufferedColor: Colors.lightGreen,
      // ),
    );
  }

  @override
  void dispose() {
    _videoPlayerController.dispose();
    _chewieController?.dispose();
    super.dispose();
  }

  Widget _renderItemVideo() {
    if (_chewieController != null && _chewieController!.videoPlayerController.value.isInitialized) {
      return Chewie(controller: _chewieController!);
    }
    return const Center(child: CircularProgressIndicator(),);
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Container(
          width: double.infinity,
          height: 600.h,
          child: _renderItemVideo(),
        ),

        TextField(
          controller: _textController,
          decoration: const InputDecoration(
            labelText: '输入网址',
          ),
        ),
        ElevatedButton(
          child: const Text('to bridge'),
          onPressed: () {
            print(_textController.text);
            String url = _textController.text.isNotEmpty ? _textController.text : 'https://www.baidu.com';
            context.push('/bridge?url=$url');
          }
        )
      ],
    );
  }
}

